package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;


    @Override
    public ResponseResult channelsList() {
        Page<WmChannel> page = new Page<>(1,100);
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        //查询全部,wrapper为null
        Page<WmChannel> channelPage = wmChannelMapper.selectPage(page, null);

        //channelPage.getRecords()得到一个List返回
        return ResponseResult.okResult(channelPage.getRecords());
    }


    //新增要去重,保证频道不能重复
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult insertChannel(WmChannel wmChannel) {

        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String name = wmChannel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道名称不能为空");

        }
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName,name);
        WmChannel dbWmChannel = wmChannelMapper.selectOne(wrapper);
        if (dbWmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在不能创建");
        }
        //创建时间前端都不传
        wmChannel.setCreatedTime(new Date());

        boolean saveResult = this.save(wmChannel);
        if (!saveResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"数据库更新失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult selectChannel(ChannelDto dto) {
        //dto判空
        if (dto == null) {
            return new PageResponseResult(0,0,0);
        }

        String name = dto.getName();
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),WmChannel::getName,name)
                .orderByDesc();

        page = (page == 0 ? 1 : page);
        size = (size == 0 ? 10 : size);

        Page<WmChannel> wmChannelPage = new Page<>(page,size);
        Page<WmChannel> resultPage = wmChannelMapper.selectPage(wmChannelPage, wrapper);

        //if (resultPage == null) {
        //    return new PageResponseResult(0,0,0);
        //}

        Long total = resultPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        pageResponseResult.setData(resultPage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {

        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"传入参数不能为空");
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId())
                .eq(WmNews::getStatus,9);
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"频道中有已发布的新闻,不能删除");
        }

        //更新数据库
        boolean updateResult = this.updateById(wmChannel);
        if (!updateResult) {
            log.warn("数据库更新失败");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {

        if (id == null) {
            log.warn("传入参数不能为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        Boolean status = wmChannel.getStatus();
        if (status) {
            log.warn("启动中的频道不能删除");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        Integer channelId = wmChannel.getId();
        if (channelId == null) {
            log.warn("频道id为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId, channelId)
                .eq(WmNews::getStatus, 9);
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"频道中存在发布状态新闻,不能删除");
        }

        boolean removeResult = this.removeById(wrapper);
        if (!removeResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"数据库更新失败");

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}